package main

import (
	"context"
	"fmt"
	"go-distributed/log"
	stdlog "log"

	"go-distributed/grades"
	"go-distributed/registry"
	"go-distributed/service"
)

func main() {
	host, port := "localhost", "6000"
	serviceAddress := fmt.Sprintf("http://%v:%v", host, port)

	reg := registry.Registration{
		ServiceName:      registry.GradingService,
		ServiceUrl:       serviceAddress,
		RequireServices:  []registry.ServiceName{registry.LogService},
		ServiceUpdateUrl: serviceAddress + "/services",
	}
	ctx, err := service.Start(context.Background(), host, port, reg, grades.RegisterHandlers)

	if err != nil {
		stdlog.Fatalln(err)
	}

	if logProvider, err := registry.GetProvider(registry.LogService); err == nil {
		fmt.Printf("Logging service found at : %s\n", logProvider)
		log.SetClientLogger(logProvider, reg.ServiceName)
	}

	<-ctx.Done()

	fmt.Println("Shutting down log service.")

}
